<?php

/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    
 * @package     _home
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class Zhishidi_Question_Block_Adminhtml_Question_Grid extends Mage_Adminhtml_Block_Widget_Grid {

    /**
     * Initialize grid settings
     *
     */
    protected function _construct() {

        parent::_construct();
        $this->setId('questionGrid');
        $this->setUseAjax(true);
        $this->setDefaultSort('question_id');
        $this->setDefaultDir('DESC');
        #$this->setSaveParametersInSession(true);
    }

    /**
     * Return Current work store
     *
     * @return Mage_Core_Model_Store
     */
    protected function _getStore() {
        $storeId = (int) $this->getRequest()->getParam('store', 0);
        return Mage::app()->getStore($storeId);
    }

    /**
     * Prepare product collection
     *
     * @return Zhishidi_Dailydeals_Block_Adminhtml_Dailydeals_Grid
     */
    protected function _prepareCollection() {

        $store = $this->_getStore();
        $collection = Mage::getModel('zhishidi_question/question')
                        ->getCollection()
                        ->joinProducts()
                        ->setOrder('question_id', 'DESC');

        #var_dump($collection);

        if ($store->getId()) {
            $collection->addStoreFilter($store->getId());
        }
        #echo $collection->getSelect()->__toString();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    /**
     * Prepare grid columns
     *
     * @return Zhishidi_Dailydeals_Block_Adminhtml_Dailydeals_Grid
     */
    protected function _prepareColumns() {
        $this->addColumn('id', array(
            'header' => Mage::helper('zhishidi_question')->__('ID'),
            'sortable' => true,
            'width' => '60px',
            'index' => 'question_id'
        ));

        $this->addColumn('product', array(
            'header' => Mage::helper('zhishidi_question')->__('Product Name'),
            'align' => 'left',
            'index' => 'product',
        ));

        $this->addColumn('name', array(
            'header' => Mage::helper('zhishidi_question')->__('Name'),
            'align' => 'left',
            'index' => 'name',
        ));

        $this->addColumn('email', array(
            'header' => Mage::helper('zhishidi_question')->__('Email'),
            'align' => 'left',
            'index' => 'email',
        ));

        $this->addColumn('question', array(
            'header' => Mage::helper('zhishidi_question')->__('Question'),
            'align' => 'left',
            'sortable' => false,
            'index' => 'question',
        ));

        $this->addColumn('created_on', array(
            'header' => Mage::helper('zhishidi_question')->__('Added On'),
            'align' => 'left',
            'type' => 'datetime',
            'index' => 'created_on',
        ));

        $this->addColumn('answered_on', array(
            'header' => Mage::helper('zhishidi_question')->__('Answered On'),
            'align' => 'left',
            'type' => 'datetime',
            'index' => 'answered_on',
        ));

        $this->addColumn('status', array(
            'header' => Mage::helper('zhishidi_question')->__('Status'),
            'index' => 'status',
            'type' => 'options',
            'options' => array(
                'hidden' => Mage::helper('zhishidi_question')->__('Disabled'),
                'public' => Mage::helper('zhishidi_question')->__('Enabled'),
            ),
        ));
        return parent::_prepareColumns();
    }

    protected function _afterLoadCollection() {
        $this->getCollection()->walk('afterLoad');
        parent::_afterLoadCollection();
    }

    protected function _filterStoreCondition($collection, $column) {
        if (!$value = $column->getFilter()->getValue()) {
            return true;
        }
        $this->getCollection()->addStoreFilter($value);
    }

    protected function _prepareMassaction() {
        $this->setMassactionIdField('question_id');
        $this->getMassactionBlock()->setFormFieldName('mass_action_question');



        $this->getMassactionBlock()->addItem('delete', array(
            'label' => Mage::helper('zhishidi_question')->__('Delete'),
            'url' => $this->getUrl('*/*/massDelete'),
            'confirm' => Mage::helper('zhishidi_question')->__('Are you sure?')
        ));


        $this->getMassactionBlock()->addItem('status', array(
            'label' => Mage::helper('zhishidi_question')->__('Change status'),
            'url' => $this->getUrl('*/*/massStatus', array('_current' => true)),
            'additional' => array(
                'visibility' => array(
                    'name' => 'status',
                    'type' => 'select',
                    'class' => 'required-entry',
                    'label' => Mage::helper('zhishidi_question')->__('Status'),
                    'values' => array(
                        'hidden' => Mage::helper('zhishidi_question')->__('Disabled'),
                        'public' => Mage::helper('zhishidi_question')->__('Enabled'),
                    ),
                )
            )
        ));
        return $this;
    }

    public function getRowUrl($row) {
        return $this->getUrl('*/*/edit', array('id' => $row->getId()));
    }

    public function getGridUrl() {
        return $this->getUrl('*/*/grid', array('_current' => true));
    }

}
